package cp1300assignment1;

import java.util.Scanner;

/**
 *
 * @author Simon Marshall // jc209673 \\
 */
public class MainUI {

    private GridModel project;

    /**
     *
     */
    public MainUI() {
        project = new GridModel();
    }

    /**
     *
     */
    public void showMenu() {
        System.out.println("=== MENU ===\n"
                + "1. Set Number of horizontal lanes (H-street) [min 1, max 3]\n"
                + "2. Set Number of vertical lanes (V-street) [min 1, max 4]\n"
                + "3. Set Probability of a car entering H-street [min 0, max 1]\n"
                + "4. Set Probability of a car entering V-street [min 0, max 1]\n"
                + "5. Run one simulation cycle\n"
                + "6. Set and run number of simulation cycles [min 1, max 10]\n"
                + "7. Exit the program \n"
                + "Enter your choice> ");
    }

    void run() {
        GridView gv = new GridView(project);
        gv.show();
        boolean exit = true;
        do {
            showMenu();
            Scanner in = new Scanner(System.in);
            int menuChoice = in.nextInt();

            if (menuChoice == 1) {
                while (true) {
                    System.out.println("Please set the number of horizontal lanes [min 1, max 3]");
                    try {
                        int choice = in.nextInt();
                        if (choice > 0 && choice <= 3) { // reprints the above sout statement if the choice is outside of the range in the if statement
                            project.sethorizontalLaneNum(in.nextInt());
                            break;
                        }
                    } catch (Exception e) { // reprints above sout statement if anything else besides 1, 2 or 3 is entered as the input choice
                    }
                }
            } else if (menuChoice == 2) {
                while (true) {
                    System.out.println("Please set the number of vertical lanes [min 1, max 4]");
                    try {
                        int choice = in.nextInt();
                        if (choice > 0 && choice < 5) { // reprints the above sout statement if the choice is outside of the range in the if statement
                            project.setverticalLaneNum(in.nextInt());
                            break;
                        }
                    } catch (Exception e) { // reprints above sout statement if anything else besides 1, 2, 3 or 4 is entered as the input choice
                    }
                }
            } else if (menuChoice == 3) {
                while (true) {
                    System.out.println("Please set the probability of a car entering H-street [min 0, max 1]");
                    try {
                        double choice = in.nextDouble();
                        if (choice >= 0 && choice <= 1) {
                            project.sethorizontalCarProbability(in.nextInt());
                            break;
                        }
                    } catch (Exception e) {
                    }
                }
            } else if (menuChoice == 4) {
                while (true) {
                    System.out.println("Please set the probability of a car entering V-street [min 0, max 1]");
                    try {
                        double choice = in.nextDouble();
                        if (choice >= 0 && choice <= 1) {
                            project.setverticalCarProbability(in.nextInt());
                            break;
                        }
                    } catch (Exception e) {
                    }
                }
            } else if (menuChoice == 5) {
                while (true) {
                    System.out.println("Run one simulation cycle");
                    try {
                        int choice = in.nextInt();
                        if (choice > 0 && choice < 5) {
                            project.setrunCycle(in.nextInt());
                            break;
                        }
                    } catch (Exception e) {
                    }
                }
            } else if (menuChoice == 6) {
                while (true) {
                    System.out.println("Set and run number of simulation cycles [min 1, max 10]");
                    try {
                        int choice = in.nextInt();
                        if (choice > 0 && choice < 11) {
                            project.setsimulationCycle(in.nextInt());
                            break;
                        }
                    } catch (Exception e) {
                    }
                }
            } else if (menuChoice == 7) {
                System.out.println("Thank you for using the Traffic Simulator");
                project.setExit(true);
            }
        } while (project.isExit() != true);
    }
}
